package com.wanwu.channel.common.util;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.SQLOutput;
import java.text.DecimalFormat;

/**
 * Created by lxy on 17/5/15.
 */
public class AmountUtil {

    private static final Logger logger = LoggerFactory.getLogger(AmountUtil.class);

    /**
     * 分转元
     *
     * @param amount
     * @return
     */
    public static String cent2yuan(Long amount) {
        if (amount == null) {
            return "";
        }
        if (amount == 0) {
            return "0";
        }
        try {
            double d = Double.parseDouble(amount.toString());
            return StringUtil.double2String(d / 100, 2);
        } catch (NumberFormatException e) {
            return "";
        }
    }

    /**
     * 分转化元
     * @param amount
     * @return
     */
    public static String centToyuan(Long amount) {
        if (amount == 0) return "0.00";
        BigDecimal fen = new BigDecimal(amount);
        BigDecimal yuan = fen.divide(new BigDecimal(100));
        return String.valueOf(yuan);
    }

    /**
     * 分转化元
     * @param amount
     * @return
     */
    public static String centToyuan(BigDecimal amount) {
        if (amount == null) return "0.00";
        BigDecimal yuan = amount.divide(new BigDecimal(100), 2, RoundingMode.HALF_UP);
        return String.valueOf(yuan);
    }


    /**
     * 元转分
     *
     * @param yuan
     * @return
     */
    public static String yuan2cent(String yuan) {
        try {
            return String.format("%.0f", new Object[]{Double.valueOf(Double.parseDouble(yuan) * 100.0D)});
        } catch (Exception e) {
        }
        return "";
    }

    /**
     * 元转分
     *
     * @param yuan
     * @return
     */
    public static Long yuan2longCent(String yuan) {
        try {
            return new BigDecimal(yuan).multiply(BigDecimal.valueOf(100)).longValue();
        } catch (Exception ignored) {
        }
        return null;
    }


    /**
     * 元转分
     *
     * @param yuan
     * @return
     */
    public static Integer yuan2IntCent(String yuan) {
        try {
            return new BigDecimal(yuan).multiply(BigDecimal.valueOf(100)).intValue();
        } catch (Exception ignored) {
        }
        return null;
    }

    /**
     * 元转分
     *
     * @param yuan
     * @return
     */
    public static Long yuan2cent(BigDecimal yuan) {
        try {
            return yuan.multiply(BigDecimal.valueOf(100L)).longValue();
        } catch (Exception e) {
            LogUtil.error(logger, e, "元转分发生异常 BigDecimal:{0}", yuan);
        }
        return null;
    }


    /**
     * Created by lxy on 17/5/15.
     */
    public static class StringUtil {

        /***
         * 根据指定小数点位数将double转为字符串
         *
         * @param d
         * @param fNumber 小数点的位数
         * @return String
         */
        public static String double2String(double d, int fNumber) {
            if (fNumber < 0) fNumber = 0;

            String pattern = null;
            switch (fNumber) {
                case 0:
                    pattern = "#0"; //$NON-NLS-1$
                    break;
                default:
                    pattern = "#0."; //$NON-NLS-1$
                    StringBuffer b = new StringBuffer(pattern);
                    for (int i = 0; i < fNumber; i++) {
                        b.append('0');
                    }
                    pattern = b.toString();
                    break;

            }
            DecimalFormat formatter = new DecimalFormat();
            formatter.applyPattern(pattern);
            String value = formatter.format(d);
            return value;
        }

        /**
         * 过滤查询条件
         * @param item
         * @return
         */
        public static String convertAdminQueryField(String item) {
            if (item == null) {
                return null;
            }else {
                item = item.trim();
                if (item.length() > 0) {
                    return item;
                }else {
                    return null;
                }
            }
        }

        /**
         * 将字符串进行脱敏处理,从startIndex到endIndex脱敏
         * @param item
         * @param startIndex
         * @param endIndex
         * @return
         */
        public static String desensitizationDeal(String item, int startIndex, int endIndex) {
            String newStr = "";
            if (item == null || startIndex > endIndex || endIndex >= item.length())
                return item;
            for (int index = 0; index < item.length(); index ++) {
                if (index >= startIndex && index <= endIndex) {
                    newStr += "*";
                } else {
                    newStr += item.substring(index, index + 1);
                }
            }
            return newStr;
        }
    }

    public static void main(String[] args) {
        System.out.println(centToyuan(1L));
        System.out.println(centToyuan(new BigDecimal("1")));
        System.out.println(cent2yuan(2L));
    }
}
